#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QKeyEvent>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QPen>
#include <QImage>
#include <QDebug>
#include <QTimer>
#include <QPoint>

#include <vector>
#include <iostream>
using namespace std;


typedef struct
{
    double x;
    double y;
    double z;
    int r,g,b;
}POINT3D;

typedef struct
{
    double x;
    double y;
}POINT;


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void keyPressEvent(QKeyEvent *ev);
    void paintEvent(QPaintEvent *);

    void RotateX(POINT3D &p, double angle);
    void RotateY(POINT3D &p, double angle);
    void RotateZ(POINT3D &p, double angle);
    POINT Projection(POINT3D p);

    void init(char imgName[], char backName[])
    {
        point_num=0;

        _angle_x = 0;
        _angle_y = 0;
        _angle_z = 0;
        viewZ=3;
        rate=1;
        pen_width=1;

        QImage img(imgName);
        w=img.width();
        h=img.height();

        p3d=(POINT3D *)malloc(sizeof(POINT3D)*(w+1)*(h+1));
        for(int i=0;i<w;i++)
        {
            for(int j=0;j<h;j++)
            {
                p3d[point_num].r=qRed(img.pixel(i,j));
                p3d[point_num].g=qGreen(img.pixel(i,j));
                p3d[point_num].b=qBlue(img.pixel(i,j));

                point_num++;
            }
        }

        back.load(backName);
    }

    void getPoints()
    {
        point_num=0;
        for(int i=0;i<w;i++)
        {
            for(int j=0;j<h;j++)
            {
                p3d[point_num].x=(1.0*i-w*0.5)/(w*0.5);
                p3d[point_num].y=(1.0*j-h*0.5)/(h*0.5);

                p3d[point_num].z=(1.0*qRed(back.pixel(i,j))-128)/128.0*rate_h;
                if(qRed(back.pixel(i,j))<=35)
                    p3d[point_num].z=0,p3d[point_num].x=0,p3d[point_num].y=0;
                point_num++;

            }
        }
    }

    POINT3D *p3d;
    int point_num;

    double _angle_x;
    double _angle_y;
    double _angle_z;

    double viewZ;

    double rate;
    int pen_width;

    double rate_h;

    int w,h;
    QImage back;

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
